<?php
$section = 'product';
$titre = _LB_PRODUCT_MANAGEMENT;
$smarty->assign("titre", $titre);

Session::setBackLink($section);

$cats = Category::getTree();

$filter = array();
$filter['NAME'] = trim(Request::get('NAME'));
$filter['ID_CATEGORY'] = Request::getIntNumber('ID_CATEGORY');
$filter['STATUS'] = Request::getIntNumber('STATUS');

$product = new Product();

$whereAdd = "TRUE";
if ($filter['NAME'] != '') {
	$whereAdd .= " AND (NAME like '%".$product->escape($filter['NAME'], TRUE)."%')";
}
if ($filter['ID_CATEGORY'] != 0) {
	$category = ($filter['ID_CATEGORY'] > 0) ? $filter['ID_CATEGORY'] : 0;
	$whereAdd .= " AND (ID_CATEGORY = {$category})";
}
if ($filter['STATUS'] != 0) {
	$status = $filter['STATUS'] - 1;
	$whereAdd .= " AND (DISABLED = {$status})";
}

$query = "SELECT * FROM PRODUCT WHERE $whereAdd ORDER BY NAME ASC";

$args = array();
$pager = new  DBPager($product,$args, false ,PAGING_DEFAULT_ITEM_PER_PAGE,$query);
$product->query($query);

$cfer = new Cfer(array(
	_LB_ACCUEIL => Utils::getHomeLink(),
	_LB_PRODUCT_MANAGEMENT => Utils::getCurrentLink())
);

$smarty->assign('cfer', $cfer);
$smarty->assign('product', $product);
$smarty->assign('filter', $filter);
$smarty->assign('cats', $cats);
$smarty->assign('pager', $pager);

$smarty->display('product_list.html');